using System;
using System.Collections;
using System.Configuration;

namespace DirectoryWatcher
{
    /// <summary>
    /// Collection of programs to execute in response to a filesystem change.
    /// </summary>
    public class ProgramToExecuteCollection : ConfigurationElementCollection
    {
        /// <summary>
        /// Numeric indexer for the collection.
        /// </summary>
        /// <param name="index">
        /// Index in the collection whose contents we are to return.
        /// </param>
        /// <returns>
        /// ProgramToExecute object at the specified index.
        /// </returns>
        public ProgramToExecute this[int index]
        {
            get
            {
                return (ProgramToExecute)base.BaseGet(index);
            }
        }

        /// <summary>
        /// String indexer for the collection; uses the Path property of each ProgramToExecute
        /// object as the index.
        /// </summary>
        /// <param name="key">
        /// Value of the Path property of the ProgramToExecute object that we are to return.
        /// </param>
        /// <returns>
        /// ProgramToExecute object with the specified path.
        /// </returns>
        public new ProgramToExecute this[string key]
        {
            get
            {
                return (ProgramToExecute)base.BaseGet(key);
            }
        }

        /// <summary>
        /// Tests to see whether a given element name is a member of this collection.
        /// </summary>
        /// <param name="elementName">
        /// Element name that we are to test.
        /// </param>
        /// <returns>
        /// True of the element name is part of this collection, false otherwise.
        /// </returns>
        protected override bool IsElementName(string elementName)
        {
            if ((string.IsNullOrEmpty(elementName)) || (elementName != "programToExecute"))
                return false;

            return true;
        }

        /// <summary>
        /// Collection type of this collection.
        /// </summary>
        public override ConfigurationElementCollectionType CollectionType
        {
            get
            {
                return ConfigurationElementCollectionType.BasicMapAlternate;
            }
        }

        /// <summary>
        /// Gets value to use as the key for a specific element in this collection.
        /// </summary>
        /// <param name="element">
        /// Element whose key we are to retrieve.
        /// </param>
        /// <returns>
        /// The Path property of the specified ProgramToExecute object.
        /// </returns>
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((ProgramToExecute)element).Path;
        }

        /// <summary>
        /// Creates a new element to add to the collection.
        /// </summary>
        /// <returns>
        /// A new ProgramToExecute object.
        /// </returns>
        protected override ConfigurationElement CreateNewElement()
        {
            return new ProgramToExecute();
        }
    }
}
